package com.amt.framework.common.pojo;

import com.amt.framework.common.constants.GlobalErrorCodeConstants;
import com.amt.framework.common.errorcode.ErrorCode;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @author zengj
 * @date 2023-12-19 17:36
 */
@Data
public class ResponseResult<T> implements Serializable {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误提示，供用户可读
     */
    private String msg;


    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setData(data);
        result.setMsg("OK");
        return result;
    }

    public static <T> ResponseResult<T> error(Integer code, String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的！");
        ResponseResult<T> result = new ResponseResult<>();
        result.code = code;
        result.msg = message;
        return result;
    }

    public static <T> ResponseResult<T> error(ResponseResult<?> result) {
        return error(result.getCode(), result.getMsg());
    }

    public static <T> ResponseResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }
}
